﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace GCT.Foundation.EntityFrameworkCore
{

    public static class FoundationTemplateDbContextConfigurer
    {
        public static readonly ILoggerFactory MyLoggerFactory
            = LoggerFactory.Create(builder => { builder.AddConsole(); });

        public static void Configure(DbContextOptionsBuilder<FoundationTemplateDbContext> builder, string connectionString)
        {
            builder.UseSqlServer(connectionString);

            ConfigureLog(builder);
        }

        public static void Configure(DbContextOptionsBuilder<FoundationTemplateDbContext> builder, DbConnection connection)
        {
            builder.UseSqlServer(connection);

            ConfigureLog(builder);
        }

        /// <summary>
        /// 配置日志,只在Debug模式下生效
        /// </summary>
        /// <param name="builder"></param>
        private static void ConfigureLog(DbContextOptionsBuilder<FoundationTemplateDbContext> builder)
        {
#if DEBUG
            builder.UseLoggerFactory(MyLoggerFactory);
#endif
        }
    }
}
